package com.hzy.admin.controller.system;

import com.hzy.admin.framework.ApiResult;
import com.hzy.admin.framework.annotation.AdminAuthorization;
import com.hzy.admin.framework.controller.ApiBaseController;
import com.hzy.admin.models.vo.system.SysUserVo;
import com.hzy.admin.models.vo.system.TableVo;
import com.hzy.admin.models.vo.system.UserInfoVo;
import com.hzy.admin.repository.entity.system.SysMenu;
import com.hzy.admin.services.system.ISysMenuService;
import com.hzy.admin.services.system.ISysUserService;
import com.hzy.admin.repository.entity.system.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 *
 * @author HZY
 */
@Api(tags = "用户控制器")
@RestController
@RequestMapping("/admin/system/user")
@AdminAuthorization("38d864ff-f6e7-43af-8c5c-8bbcf9fa586d")
public class SysUserController extends ApiBaseController<ISysUserService> {
    private ISysMenuService sysMenuService;

    public SysUserController(ISysUserService service, ISysMenuService sysMenuService) {
        super(service);
        this.sysMenuService = sysMenuService;
    }

    /**
     * 查询列表
     *
     * @param page   页码
     * @param size   页数
     * @param search 查询
     * @return json
     */
    @ApiOperation("查询列表")
    @PostMapping("/findList/{size}/{page}")
    @ResponseBody
    public ApiResult<?> findList(@PathVariable Integer page, @PathVariable Integer size, @RequestBody SysUser search) {
        TableVo<Map<String,Object>> tableViewDto = this.service.findList(page, size, search);
        return this.resultOk(tableViewDto);
    }

    /**
     * 根据 id 查询表单信息
     *
     * @param id id
     * @return json
     */
    @ApiOperation("根据 id 查询表单信息")
    @GetMapping({"/findForm/{id}", "/findForm"})
    @ResponseBody
    public ApiResult<?> findForm(@PathVariable(required = false) String id) {
        return this.resultOk(this.service.findForm(id));
    }

    /**
     * 保存数据
     *
     * @param form 要保存数据
     * @return json
     */
    @ApiOperation("保存数据")
    @PostMapping("/saveForm")
    @ResponseBody
    public ApiResult<?> saveForm(@RequestBody SysUserVo form) {
        return this.resultOk(this.service.saveForm(form));
    }

    /**
     * 删除
     *
     * @param ids id集合
     * @return json
     */
    @ApiOperation("删除")
    @PostMapping("/deleteList")
    @ResponseBody
    public ApiResult<?> deleteList(@RequestBody List<String> ids) {
        this.service.deleteList(ids);
        return this.resultOk();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    @ResponseBody
    public ApiResult<?> getUserInfo() {
        UserInfoVo userInfo = this.service.getUserInfo();
        List<SysMenu> sysMenus = this.sysMenuService.getMenusByCurrentRole();
        /**
         * 设置菜单 Map
         */
        List<Map<String,Object>> sysMenusMap = this.sysMenuService.menusToListMap("", sysMenus);
        userInfo.setMenus(sysMenusMap);
        /**
         * 设置菜单权限
         */
        userInfo.setMenuPowers(this.sysMenuService.getPowerByMenus(sysMenus));
        return this.resultOk(userInfo);
    }

}
